<?php
	
	class View {
		
		function parse($file){
			//global $__view_vars,$__view_last_varname;
			//ob_clean();
			//ob_flush();
			$prev_buffer = ob_get_contents();
			if(!empty($prev_buffer)){
				ob_end_clean();
			}
			$this->_last_varname=NULL;
			$this->_parse_file=$file;
			include($file);
			//view_var("_end");
			$this->_vars[$this->_parse_file][$this->_last_varname]=ob_get_contents();
			ob_end_clean();

			foreach($this->_vars[$file] as $k=>$v) $this->$k=$v;
			if(is_string($prev_buffer)) $prev_buffer = trim($prev_buffer);
			if(!empty($prev_buffer)){
				ob_start();
				echo $prev_buffer;
			}
		}
		
		function parseLang($file){
			
			$prev_buffer = ob_get_contents();
			if(!empty($prev_buffer)){
				ob_end_clean();
			}
			$this->_last_varname=NULL;
			$this->_parse_file=$file;
			include($file);
			
			//view_var("_end");
			
			$this->_vars[$this->_parse_file][$this->_last_varname]=ob_get_contents();
			ob_end_clean();
			
			$this->_lang= array_merge($this->_lang,$_lang);
			$this->_lang = array_merge($this->_lang,$this->_vars[$this->_parse_file]);
			
			if(is_string($prev_buffer)) $prev_buffer = trim($prev_buffer);
			if(!empty($prev_buffer)){
				ob_start();
				echo $prev_buffer;
			}
			
		}
		
		function assign($data){
			foreach($data as $k=>$v) $this->$k=$v;
		}
		
		function render($file){
			ob_start();
			include($file);
			$content = ob_get_contents();
			ob_clean();
			ob_end_flush();
			return $content;
		}
		
		function renderFromString($string){
			ob_start();
			$string = addslashes($string);
			eval("echo \"$string\" ;");
			$content = ob_get_contents();
			ob_clean();
			ob_end_flush();
			return $content;
		}
		
		function startvar($var){
			$this->endvar();
			ob_start();
			$this->_last_varname=$var;
		}
		
		function startContent($var){
			$this->startvar($var);
		}
		
		function startLang($tag){
			$this->startvar($tag);
		}
		
		function endvar(){
			if(isset($this->_last_varname)){
				$buffer = ob_get_contents();
				$this->_vars[$this->_parse_file][$this->_last_varname]= $buffer;
				ob_clean();
			}
			$this->_last_varname=NULL;
			return $buffer;
		}
		
		function getvar($name){
			return $this->_vars[$this->_parse_file][$name];
		}
		
	}
	
	
	
	$__view_vars=array();
	$__view_last_varname=NULL;
	
	function view_var($var){
		//global $__view,$__view_vars,$__view_last_varname;
		
		if(isset($__view_last_varname)){
			$this->_vars[$__view->getContentFile()][$__view_last_varname]=ob_get_contents();
			ob_clean();
		}
		
		ob_start();
		$__view_last_varname=$var;
	}
?>